import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";

//项目当中使用矢量图
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";

//引入mock
import { UserConfigExport, ConfigEnv } from "vite";

export default ({ command, mode }: ConfigEnv): UserConfigExport => {
  // 环境处理
  const env = loadEnv(mode, process.cwd(), "");
  return {
    define: {},
    plugins: [
      createSvgIconsPlugin({
        // 指定需要缓存的图标文件夹
        iconDirs: [path.resolve(process.cwd(), "src/assets/icons")],
        // 指定symbolId格式
        symbolId: "icon-[dir]-[name]",
      }),
      vue(),
      // viteMockServe({
      //   mockPath: "mock",
      //   //开发阶段可以使用mock接口
      //   localEnabled: command === "serve",
      // }),
    ],
    resolve: {
      alias: {
        // src 声成别名路径
        "@": path.resolve("./src"),
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          javascriptEnabled: true,
          additionalData: '@import "./src/styles/variable.scss";',
        },
      },
    },
    //服务器配置相关项
    server: {
      port: parseInt(env.VITE_PORT),
      //代理服务器信息
      proxy: {
        //前置信息
        [env.VITE_APP_BASE_API_COMM]: {
          //代理服务地址
          target: env.VITE_APP_SERVER_URL,
          //需要代理跨域
          changeOrigin: true,
          //路径重写处理
          rewrite: (path) => path.replace(/^\/api/, ""),
        },
      },
    },
  };
};
